import {SAVE_FUNCTIONALITIES, GET_FUNCTIONALITIES, GET_EQUIPMENT_TYPE, SAVE_EQUIPMENT_TYPE, GET_TAG_TREE, SAVE_TAG_TREE } from '__STORE__/operation-type';
import {isPC} from '__UTILS__/user-testing.js'
//获取tagTree
import { getTags } from '__SERVICE__/tag.js'
//获取功能性标签
import {getFunctionalities} from '__SERVICE__/functionality.js'

const initEquipmentType = 'PC' //初始的设备信息，默认是PC

//自定以的功能路由
const initMineFunctionRoute = [{
  name: '创作文章',
  cover_url: 'https://th.wallhaven.cc/small/2e/2em38y.jpg',
  route: {name: 'CreateArticle'}
},{
  name: '信息版面',
  cover_url: 'https://th.wallhaven.cc/lg/xl/xlx8ez.jpg',
  route: {name: 'PreviewUser'}
},{
  name: '修改信息',
  cover_url: 'https://th.wallhaven.cc/small/83/832grj.jpg',
  route: {name: 'ChangeInfo'}
},{
  name: '修改密码',
  cover_url: 'https://th.wallhaven.cc/small/zm/zmm7mw.jpg',
  route: {name: 'ChangePassowrd'}
}]

//自定义的开发者信息
const initAuthor = {
  qq: {
    name: '蛇皮鸡',
    portrait: 'https://i.loli.net/2020/11/07/c3sCogNVXTw71Fx.jpg',
    code: 'https://i.loli.net/2020/11/07/Z2aWcixkqyfnj3l.jpg',
    number: '2020974511'
  },
  wechat: {
    name: '蛇皮鸡',
    portrait: 'https://i.loli.net/2020/11/07/c3sCogNVXTw71Fx.jpg',
    code: 'https://i.loli.net/2020/11/07/SYxqwVleJ1IQmbr.jpg',
    number: '13026239138'
  }
}

//初始化tag树
const initTagTree = null


//初始化tag树
const initFunctionalities = null

const state = ()=>({
  equipmentType: initEquipmentType,
  author: Object.freeze(initAuthor),
  mineFunctionRoute: Object.freeze(initMineFunctionRoute),
  tagTree: initTagTree,
  functionalities: initFunctionalities
})

const getters = {
  tags(state) {
    const { tagTree } = state
    if(!tagTree) return []
    let tags = []
    tagTree.forEach((tagclass)=>{
      tags = tags.concat(tagclass.tag_models)
    })
    
    return Object.freeze(tags)
  }
}

const mutations = {
  [SAVE_EQUIPMENT_TYPE](state, payload) {
    state.equipmentType = payload
  },
  [SAVE_TAG_TREE](state, payload) {
    state.tagTree = payload
  },
  [SAVE_FUNCTIONALITIES](state, payload) {
    state.functionalities = payload
  },
}

const actions = {
  //初始化
  [GET_EQUIPMENT_TYPE](context, payload) {
    const {commit, state} = context;
    const type = isPC() ? 'PC' : 'Phone'
    commit(SAVE_EQUIPMENT_TYPE, type);
  },
  async [GET_TAG_TREE](context, payload) {
    const {commit, state} = context;
    const result = await getTags()
    const {data: {tagclasses}} = result
    tagclasses && tagclasses[0] && (tagclasses[0].tag_models = Object.freeze(tagclasses[0].tag_models))
    commit(SAVE_TAG_TREE, Object.freeze(tagclasses));
  },
  async [GET_FUNCTIONALITIES](context, payload) {
    const {commit, state} = context;
    const result = await getFunctionalities()
    const {data: {functionalities}} = result
    commit(SAVE_FUNCTIONALITIES, Object.freeze(functionalities));
  }
}

const globalConfig = {
  state,
  mutations,
  getters,
  actions
};

export default globalConfig;